package com.dhc.quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Date;


public class JobSimpleTrigger implements org.quartz.Job {

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        Trigger trigger = context.getTrigger();
        trigger.getKey().getName();//触发器名
        System.out.println(trigger.getKey().getGroup());//触发器组名
        trigger.getStartTime();//获取触发器开始时间
        System.out.println(trigger.getEndTime());//获取触发器结束时间

    }

    public static void main(String[] args) throws Exception{
        //Scheduler调度器 工厂中获取调度实例
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

        //设置开始时间
        Date startDate = new Date();
        //设置结束时间
        Date endDate = new Date();

        //Job任务实例
        JobDetail jobDetail = JobBuilder.newJob(JobSimpleTrigger.class)
                .withIdentity("job1", "jobGroup1")//1任务名2组名
                .usingJobData("jobMsg","jobMsg")
                .usingJobData("count",0)
                .build();
//        jobDetail.getKey().getName();//任务名
//        jobDetail.getKey().getGroup();//组名(未设置显示DEFAULT)
//        System.out.println(jobDetail.getJobClass().getName());//com.dhc.quartz.Job
        //Trigger触发器
        Trigger simpleTrigger = TriggerBuilder.newTrigger()
                .withIdentity("trigger1", "triggerGroup1")
                .startAt(startDate)
                .endAt(endDate)
                .build();
        //让调度器关联任务和触发器
        scheduler.scheduleJob(jobDetail,simpleTrigger);
        //启动
        scheduler.start();
    }
}
